/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import Beans.Videos;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
/**
 *
 * @author Alex
 */
public class VideosDao {
    
    
    Connection con=null;
    
    public void setConection( Connection con){
        this.con = con;
    }
    
    public boolean existe_Video(Videos videos) throws SQLException
	 {
		ResultSet re=null;
		PreparedStatement ps=null;
		boolean bandera=false;
		try
		{
			ps= con.prepareStatement("Select * from videos where id_video=?");
                        ps.setString(1, videos.getIdvideo());
			re=ps.executeQuery();
			while(re.next())
			{
				bandera=true;
			}
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return bandera;
	}
	
	//Adicionar elementos a la BD

	public void newVideo(Videos videos) throws SQLException
	{
		if(existe_Video(videos))
			throw new SQLException("El Video Ya Existe");

		PreparedStatement pstmt = null;
		try
		{
			pstmt = con.prepareStatement("INSERT INTO videos VALUES(?,?,?,?,?,?,?)");
			pstmt.setString(1,videos.getIdvideo());
			pstmt.setString(2,videos.getNombrevideo());
                        pstmt.setDate(3,videos.getFechavideo());
                        pstmt.setString(4,videos.getDirectorvideo());
                        pstmt.setString(5,videos.getPaisvideo());
                        pstmt.setString(6,videos.getGenerovideo());
                        pstmt.setString(7,videos.getIdproductora());

			pstmt.executeUpdate();
		}
		finally
		{
			if(pstmt != null)
				pstmt.close();
		}
	}
        
        public List listar()throws SQLException{
            	ResultSet re=null;
		PreparedStatement ps=null;
		List lista = new LinkedList();
		try
		{
			ps=(PreparedStatement) con.prepareStatement("Select * from videos");
                        re=ps.executeQuery();
			while(re.next())
			{
				lista.add(Videos.load(re));
			}
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return lista;
        }
        
        
        public List buscar(int tipo, String parametro)throws SQLException{
            	ResultSet re=null;
		PreparedStatement ps=null;
		List lista = new LinkedList();
                String sql="";
		try
		{
                    if(tipo==1)
                        sql="Select * from videos where id_video like ?"; 
                     if(tipo==2)
                        sql="Select * from videos where nom_video like ?"; 
                    
			ps=(PreparedStatement) con.prepareStatement(sql);
                        ps.setString(1,parametro);
                        re=ps.executeQuery();
			while(re.next())
			{
				lista.add(Videos.load(re));
			}
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return lista;
        }
        
        public void eliminar(String cod)throws SQLException{
                PreparedStatement pstmt = null;
		try
		{
			pstmt =con.prepareStatement("delete from videos where id_video=?");
			pstmt.setString(1,cod);
			pstmt.executeUpdate();
		}
		finally
		{
			if(pstmt != null)
				pstmt.close();
		}
        }
        
        public void modificar(Videos p)throws SQLException{
            

		PreparedStatement pstmt = null;
		try
		{
			pstmt =con.prepareStatement("Update videos set nom_video = ?, fecha_video = ?, director_video, pais_video = ?, genero_video=? where id_video = ?");
			pstmt.setString(2,p.getNombrevideo());
                        pstmt.setDate(3,p.getFechavideo());
                        pstmt.setString(4,p.getDirectorvideo());
                        pstmt.setString(5,p.getPaisvideo());
                        pstmt.setString(6,p.getGenerovideo());
                        pstmt.setString(7,p.getIdvideo());
                        pstmt.setString(8,p.getIdproductora());
                        pstmt.executeUpdate();
		}
		finally
		{
			if(pstmt != null)
				pstmt.close();
		}
            
        }
        
        public Videos obtener(String cod)throws SQLException{
            ResultSet re=null;
		PreparedStatement ps=null;
		Videos p = new Videos();
		try
		{
			ps=con.prepareStatement("Select * from videos where id_video=?");
                        re=ps.executeQuery();
			while(re.next())
			{
				p = Videos.load(re);
			}
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return p;
        }
        
         public Videos getVideos(String id)throws SQLException{
            	ResultSet re=null;
		PreparedStatement ps=null;
		Videos p = new Videos();
		try
		{
			ps=con.prepareStatement("Select * from videos where id_video=?");
                        ps.setString(1, id);
                        re=ps.executeQuery();
			if(re.next())
			
				p = Videos.load(re);
			
		}
		catch(SQLException ignore)
		{

		}
		finally
		{
			if(re!=null)
			{
				re.close();
			}
			if(ps!=null)
			{
				ps.close();
			}
		}
		return p;
        }
            public static Videos load(ResultSet filas) throws Exception{

       try {

            Videos e = new Videos();
            e.setIdvideo(filas.getString("idvideo"));
            e.setNombrevideo(filas.getString("nombrevideo"));
            e.setFechavideo(filas.getDate("fechavideo"));
            e.setDirectorvideo(filas.getString("directorvideo"));
            e.setPaisvideo(filas.getString("paisvideo"));
            e.setGenerovideo(filas.getString("generovideo"));
            e.setIdproductora(filas.getString("idproductora"));
            return e;
        } catch (SQLException ex) {
            throw new Exception("Error en la Consulta del Video:\n" + ex.getMessage());
        }

    }
    
}
